package QQCilent.Service;

import com.wyedu.qqcommon.Message;
import com.wyedu.qqcommon.MessageType;
import com.wyedu.qqcommon.User;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

public class MessageClientServer implements MessageType{
    /**
     *
     * 该类负责和服务端进行消息直接的发送
     */
    private User u = null;
    private Socket  socket = null;
    private Scanner scanner = null;

    public User getU() {
        return u;
    }

    public void setU(User u) {
        this.u = u;
    }

    public Socket getSocket() {
        return socket;
    }

    public void setSocket(Socket socket) {
        this.socket = socket;
    }

    public Scanner getScanner() {
        return scanner;
    }

    public void setScanner(Scanner scanner) {
        this.scanner = scanner;
    }

    public void DirectMessage() {

        try {
            ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
            while (true){
                Message message = new Message();
                //消息类型
                message.setMesType(MessageType.MESSAGE_COMM_MES);
                //发送消息的人
                message.setSender(u.getUserId());
                int n;
                String name,news;
                    System.out.println("请输入要发送给谁");
                    System.out.print("->");
                    //接受消息的人
                     name = scanner.next();
                    message.setGetter(name);
                    //要发生的消息
                    scanner.nextLine();
                    System.out.println("请输入要发送的消息");
                    news = scanner.nextLine();
                    message.setContent(news);

                    System.out.println("是否确认发送? 1.确认发送 2.重新编辑 3.退出");
                    System.out.print("->");
                     n = scanner.nextInt();
                     scanner.nextLine();


                switch (n){
                    case 1:
                        //发送消息
                        oos.writeObject(message);
                        //打印我这里发送的消息
                        System.out.println(" 我对("+name + ")说:" + news);
                        return;
                    case 2:
                        break;
                    case 3:
                        return;

                }
        }} catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void groupChat(){
        //群聊

        try {
            ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
            while (true){

                Message message = new Message();
                //消息类型
                message.setMesType(MessageType.MESSAGE_COMM_MES_ALL);
                //发送消息的人
                message.setSender(u.getUserId());
                message.setGetter(u.getUserId());
                //要发送的消息
                System.out.println("请输入要发送的消息");
                String news = scanner.nextLine();
                message.setContent(news);

                System.out.println("是否确认发送? 1.确认发送 2.重新编辑 3.退出");
                System.out.print("->");
                int n = scanner.nextInt();
                scanner.nextLine();

                switch (n){
                    case 1:
                        //发送消息
                        oos.writeObject(message);
                        //打印我这里发送的消息
                        System.out.println(" 我对(所有人)说:" + news);
                        return;
                    case 2:
                        break;
                    case 3:
                        return;

                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void SendFile() {
        //负责发送文件
        System.out.print("请输入要发送给的用户->");
        String id = scanner.nextLine();
        System.out.print("请输入要传输的文件路径-> ");
        File file = new File(scanner.nextLine());

        if(file.isFile()){
            //判断是否是普通文件
            try {
                BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
                Message message = new Message();
                //不断的写入到bis中
                byte[] b = new byte[4096];
                int n = 0;
                while ((n = bis.read(b,0,4096))!= -1){
                    baos.write(b);
                }
                byte[]array = baos.toByteArray();
                //1.指定消息类型
                message.setMesType(MessageType.MESSAGE_COMM_FILE);
                //2.指定发送用户
                message.setSender(u.getUserId());
                //3.指定接收用户
                message.setGetter(id);
                //4.将二进制数组添加到该对象中
                message.setFileMessage(array);
                //5.将文件名字存入
                message.setContent(file.getName());
                //发送该对象
                oos.writeObject(message);
                //在这里阻塞

            }catch (Exception e){
                e.printStackTrace();
            }
        }else {
            System.out.println("文件路径错误");
        }
    }
}
